/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2p.raca.Server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author p
 */
public final class PeerFileInformation {
    
    public int fileSize;
    private Vector whoHas;
    private String name;
    

    public PeerFileInformation(String name, int fileSize){
        this.name = name;
        try {
            LoadPFI(name);
            this.fileSize=fileSize; 
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
     public PeerFileInformation(String name){
        this.name = name;
         try {
             
            LoadPFI(name);
            this.fileSize=-1;
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
    
    public void addHost(String host){
        whoHas.add(host);
    }
    
    public void remHost(String host){
        whoHas.remove(host);
    }
    public int getWhoHasSize(){
        return whoHas.size();
    
    }
    public String getHost(){
        return (String)whoHas.firstElement();
    }
    
    public String getHost(int i){
        return (String) whoHas.elementAt(i);
    }
    
    public void savePFI(String nome) throws IOException {
        File arquivo = new File("shared/"+nome+".pfi");
        if (arquivo.exists())
            arquivo.delete();
        arquivo.createNewFile();
        FileWriter fw = new FileWriter(arquivo, true);
        PrintWriter out = new PrintWriter(fw);
        out.println(fileSize);
        for (int i=0; i<whoHas.size();i++)
        {
            out.println(whoHas.elementAt(i));
            out.flush();
        }
    }
    
    public void LoadPFI(String nome) throws FileNotFoundException, IOException
        {
            
            
            
            File arquivo = new File("shared/"+nome+".pfi");
            if (arquivo.exists())
                {
                whoHas = new Vector();
                BufferedReader in = new BufferedReader(new FileReader(arquivo));

                fileSize = Integer.parseInt(in.readLine());

                while(in.ready())
                {
                    whoHas.add(in.readLine());
                }
                
                in.close();
                }else
                    {
                        whoHas = new Vector();

                    }
          }
    public boolean searchPFIfor(String host){
        for(int i =0 ; i < whoHas.size(); i++){
            if(host.equals(whoHas.elementAt(i)))
                    return true;
        }
        return false;
    }
    
   public void printPFI()
   {
       System.out.println("Informacao sobre o arquivo:" + this.name);
       System.out.println("Tamanho: " + fileSize);
       System.out.println("Hosts que possuem:");
       for (int i=0; i<whoHas.size();i++)
           System.out.println(i+": "+whoHas.elementAt(i));
               }
  
}
